package com.partners.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.service.WxPayService;
import com.partners.annotation.BeanValid;
import com.partners.constant.SuccessMessageEnum;
import com.partners.dao.bo.GroupOrderDO;
import com.partners.dao.dto.*;
import com.partners.model.ApiResult;
import com.partners.service.PayService;
import com.partners.utils.ApiResultUtil;
import com.partners.utils.RequestUtils;
import com.partners.utils.SessionManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 支付管理
 * @author liangyongle
 * @date 2019-01-12
 */
@RestController
@RequestMapping("pay")
@Slf4j
public class PayController {


    @Resource
    private PayService payService;

    @Resource
    private WxPayService wxPayService;


    /**
     * 下订单
     * @param  order OrderInputDTO
     * */
   @PostMapping("create_order")
   public ApiResult<GroupOrderDO> createOrder(@BeanValid @RequestBody OrderInputDTO order, HttpServletRequest request){
       log.info("request{}",request);
       GroupOrderDO groupOrderDO =  payService.createGroupOrder(order);
       return ApiResultUtil.ok(groupOrderDO);
   }




    /**
     * 微信支付
     * */
    @PostMapping("pay")
    public ApiResult<PayOrderOutputDTO> payOrder(@BeanValid @RequestBody PayOrderInputDTO input
        ,HttpServletRequest request){
        String ip = RequestUtils.getIpAddress(request);
        PayOrderOutputDTO result = payService.createPayInfo(input.getOrderId(),ip);
        return ApiResultUtil.ok(result);
    }


    /**
     * 微信通知支付结果的回调地址，notify_url
     *
     * @param request
     * @param response
     */
    @GetMapping("/payCallBack")
    public String payNotify(HttpServletRequest request, HttpServletResponse response) {

        log.info("支付回调{}",response.getStatus());
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);
            log.info("result{}",result);
            // 结果正确
            String orderNo = result.getOutTradeNo();
            String tradeNo = result.getTransactionId();
            log.info("orderNo{}",orderNo);
            Integer totalFee = result.getTotalFee();
            String timeEnd = result.getTimeEnd();
            //自己处理订单的业务逻辑，需要判断订单是否已经支付过，否则可能会重复调用
            //orderService.paySuccess(orderNo, tradeNo, totalFee, timeEnd);

            return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            log.error("微信回调结果异常,异常原因{}", e.getMessage());
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }

    /**
     * 提现
     * @param entPaymentInputDTO 入站dto
     * @return Map
     */
    @PostMapping("ent_payment")
    public ApiResult entPayment(@BeanValid @RequestBody EntPaymentInputDTO entPaymentInputDTO, HttpServletRequest request) {

        String ip = RequestUtils.getIpAddress(request);
        payService.entPayment(entPaymentInputDTO.getDraw_amount(), ip);
        return ApiResultUtil.ok(SuccessMessageEnum.OPERATION_SUCCESS);
    }

    /**
     * 同意退款/拒绝退款
     * @param input RefundOrderInputDTO
     * @return ApiResult
     */
    @PostMapping("/refund_order")
    public ApiResult refundOrder(@RequestBody RefundOrderInputDTO input) {
        payService.applyRefund(input.getOrderNo());
        return ApiResultUtil.ok(null);
    }




}
